package com.gui.admin;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.controller.BugCon;
import com.gui.baseframe.BaseFrame;
import com.gui.login.LoginAll;
import com.tool.ConnTools;
import com.tool.DatabaseTool;

public class AlterAdmin extends BaseFrame {
	private JFrame jf;
	private String name;
	private static final int GRID_ROWS = 6;
	private static final int GRID_COLS = 2;
	// 文本框长度
	private static final int TEXT_COLUMNS = 14;
	// 边框距离
	private static final int BORDER_LENGTH = 20;

	// 下拉列表框的下标
	public int i1, i2, i3;

	// 三个下拉列表框
	private JComboBox<String> jcb8;
	private JComboBox<String> jcb9;
	private JComboBox<String> jcb10;
	// 书架编号
	public String str = "";
	// 判断文本框是否为日期和数值
	private static boolean isbug = false;

	public AlterAdmin(String name) {
		this.name = name;
		JPanel jp = createJP3();
		this.add(jp);
		this.init();
	}
	/**
	 * 更改信息
	 * 
	 * @return
	 */
	private JPanel createJP3() {
		// 设置一个总容器
		JPanel jp = new JPanel();
		jp.setLayout(new BorderLayout());
		// 创建一个顶部容器
		JPanel topjp = new JPanel();
		JLabel jlab = new JLabel("欢迎登陆图书管理系统——更改信息");
		topjp.add(jlab);
		// 创建一个中间容器
		JPanel centerjp = new JPanel();
		centerjp.setLayout(new GridLayout(GRID_ROWS, GRID_COLS));

		// 组2
		JPanel jp_rows2 = new JPanel();
		JLabel jl2 = new JLabel("名　称　：");
		JTextField jtf2 = new JTextField(TEXT_COLUMNS);
		jp_rows2.add(jl2);
		jp_rows2.add(jtf2);
		// 组3
		JPanel jp_rows3 = new JPanel();
		JLabel jl3 = new JLabel("类　别　：");
		JTextField jtf3 = new JTextField(TEXT_COLUMNS);
		jp_rows3.add(jl3);
		jp_rows3.add(jtf3);
		// 组4
		JPanel jp_rows4 = new JPanel();
		JLabel jl4 = new JLabel("价　格　：");
		JTextField jtf4 = new JTextField(TEXT_COLUMNS);
		jp_rows4.add(jl4);
		jp_rows4.add(jtf4);
		ActionBug(jtf4, jl4);
		// 组5
		JPanel jp_rows5 = new JPanel();
		JLabel jl5 = new JLabel("数　量　：");
		JTextField jtf5 = new JTextField(TEXT_COLUMNS);
		jp_rows5.add(jl5);
		jp_rows5.add(jtf5);
		ActionBug(jtf5, jl5);
		// 组6
		JPanel jp_rows6 = new JPanel();
		JLabel jl6 = new JLabel("出版社　：");
		JTextField jtf6 = new JTextField(TEXT_COLUMNS);
		jp_rows6.add(jl6);
		jp_rows6.add(jtf6);
		// 组7
		JPanel jp_rows7 = new JPanel();
		JLabel jl7 = new JLabel("入库时间：");
		JTextField jtf7 = new JTextField(TEXT_COLUMNS);
		jp_rows7.add(jl7);
		jp_rows7.add(jtf7);
		// 添加所有控件
		centerjp.add(jp_rows2);
		centerjp.add(jp_rows3);
		centerjp.add(jp_rows4);
		centerjp.add(jp_rows5);
		centerjp.add(jp_rows6);
		centerjp.add(jp_rows7);
		// 创建底部容器
		JTextField text[] = new JTextField[] { jtf2, jtf3, jtf4, jtf5, jtf6, jtf7 };
		ConnTools.getList(text); // 集合添加方法
		Map<String, String> map = new HashMap<String, String>();
		JPanel botjp = new JPanel();
		botjp.setLayout(new BorderLayout());
		JButton jb = new JButton("更新");
		jb.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String nowDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
				String sqls = ConnTools.getMap(text, map); // 返回sql语句方法
				if (isbug) {
					JOptionPane.showMessageDialog(null, "你必须输入正确的内容");
					return;
				} else {
					String sql = "UPDATE book set " + sqls + " where b_name='" + name + "'";
					String insertsql = "insert into info(i_title,i_content,i_date,a_name) values('修改图书'," + "'修改了"
							+ text[3].getText() + "本" + text[0].getText() + "书','" + nowDate + "','" + LoginAll.getUser() + "')";
					DatabaseTool.doUpdate(sql); // 修改语句方法
					DatabaseTool.doInsert(insertsql);
				}
			}
		});
		botjp.add(jb, BorderLayout.SOUTH);
		// 添加所有组
		jp.add(topjp, BorderLayout.NORTH);
		jp.add(centerjp, BorderLayout.CENTER);
		jp.add(botjp, BorderLayout.SOUTH);
		return jp;
	}

	public void init() {
		super.init();
		this.setTitle("图书信息修改");
		this.setLocationRelativeTo(null);
		this.setSize(500, 500);
		this.setDefaultCloseOperation(HIDE_ON_CLOSE);
	}

	/**
	 * 数字和日期文本框的正则判断
	 * 
	 * @param jtf
	 *            获取文本框
	 * @param jl
	 *            获取标签
	 * @return
	 */
	public void ActionBug(JTextField jtf, JLabel jl) {

		jtf.addKeyListener(new KeyListener() {
			@Override
			public void keyTyped(KeyEvent e) {
			}

			@Override
			public void keyReleased(KeyEvent e) {
			}

			@Override
			public void keyPressed(KeyEvent e) {
				// 调用BugCon的actionBug方法，将文本框，标签，以及标签的文字内容取出来
				isbug = BugCon.actionBug(jtf, jl, jl.getText().replaceAll("　| |：", ""));
			}
		});
	}
}
